package cc.chinagps.gateway.memcache;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.meetup.memcached.MemcachedClient;
import com.meetup.memcached.SockIOPool;

import cc.chinagps.gateway.util.SystemConfig;

public class MemcacheManager {
	private static MemcachedClient client;

	public static String ROUTING_KEY_HEAD;

	public static String BASE_INFO_KEY_HEAD;

	public static String IMEI_CALLLETTER_KEY_HEAD;

	public static String BASE_STATION_INFO_KEY_HEAD;

	public static long ROUTING_EXPIRED;

	public static String OBD_ADAPT_KEY_HEAD = "obd_adapt:";

	public static String KELONG_CAN_DATA_HEAD = "kelong_can_data:";

	public static String ICCID_CALLLETTER_KEY_HEAD = "iccid:";

	static {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("[" + sdf.format(new Date()) + "]init memcache start");
		try {
			ROUTING_KEY_HEAD = SystemConfig.getMemcacheProperty("routing_key_head");
			ROUTING_EXPIRED = Long.valueOf(SystemConfig.getMemcacheProperty("routing_expired"));
			BASE_INFO_KEY_HEAD = SystemConfig.getMemcacheProperty("base_info_key_head");
			IMEI_CALLLETTER_KEY_HEAD = SystemConfig.getMemcacheProperty("imei_callletter_key_head");
			BASE_STATION_INFO_KEY_HEAD = SystemConfig.getMemcacheProperty("base_station_info_key_head");
			String obdAdapt = SystemConfig.getMemcacheProperty("obd_adapt_key_head");
			if (obdAdapt != null)
				OBD_ADAPT_KEY_HEAD = obdAdapt;
			String kelongCanData = SystemConfig.getMemcacheProperty("kelong_can_data_head");
			if (kelongCanData != null) {
				KELONG_CAN_DATA_HEAD = kelongCanData;
			}

			String[] servers = SystemConfig.getMemcacheProperty("servers").split(",");
			SockIOPool pool = SockIOPool.getInstance();
			pool.setServers(servers);
			pool.setFailover(Boolean.valueOf(SystemConfig.getMemcacheProperty("failover")));
			pool.setInitConn(Integer.valueOf(SystemConfig.getMemcacheProperty("initConn")));
			pool.setMinConn(Integer.valueOf(SystemConfig.getMemcacheProperty("minConn")));
			pool.setMaxConn(Integer.valueOf(SystemConfig.getMemcacheProperty("maxConn")));
			pool.setNagle(Boolean.valueOf(SystemConfig.getMemcacheProperty("nagle")));
			pool.setSocketTO(Integer.valueOf(SystemConfig.getMemcacheProperty("socketTO")));
			pool.setAliveCheck(Boolean.valueOf(SystemConfig.getMemcacheProperty("aliveCheck")));
			pool.initialize();

			client = new MemcachedClient();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("[" + sdf.format(new Date()) + "]init memcache end");
	}

	public static MemcachedClient getMemcachedClient() {
		return client;
	}

	public static void main(String[] args) {
		// 14548995049 1064849396341 obd_adapt:14570411749 obd_adapt:14570430991
		String callLetter = "36102420005";
		

		String imei = "13012345678";
		imei = "36102420005";
		System.out.println(MemcacheManager.getMemcachedClient().get("imei:" + imei));
		System.out.println(MemcacheManager.getMemcachedClient().get("obd_adapt:" + callLetter));
		System.out.println(MemcacheManager.getMemcachedClient().get("kelong_can_data:" + callLetter));
		String iccid = "89860469102080112012";
		System.out.println(MemcacheManager.getMemcachedClient().get(ICCID_CALLLETTER_KEY_HEAD + iccid));
	
		System.out.println(MemcacheManager.getMemcachedClient().get("data:" + callLetter));
		System.out.println("routing:" + MemcacheManager.getMemcachedClient().get(ROUTING_KEY_HEAD + callLetter));
		imei = "102080112012";
		//System.out.println(MemcacheManager.getMemcachedClient().set(ICCID_CALLLETTER_KEY_HEAD + iccid,imei));
		//System.out.println(MemcacheManager.getMemcachedClient().set("imei:" + imei,imei));
	}
}